$(function() {
	$(".select-all").click(function() {
		var c = $(this).children("input").attr("checked");
		if(c == "checked") {
			$(".select-all i").css("background-image", "url(img/u3.png)");
		} else {
			$(".select-all i").css("background-image", "url(img/u2.png)");
		}
	});
});

window.onload = function() {
	var use = document.getElementById("use");
	var core = document.getElementById("core");

	use.onmousemove = function() {
		core.style.display = "block";
	}
	core.onmousemove = function() {
		core.style.display = "block";
	}
	use.onmouseout = function() {
		core.style.display = "none";
	}
	core.onmouseout = function() {
		core.style.display = "none";
	}

	var table = document.getElementById('cartTable'); // 购物车表格	
	var selectInputs = document.getElementsByClassName('check'); // 所有勾选框
	var checkAllInputs = document.getElementsByClassName('check-all') // 全选框
	var checkone = document.getElementsByClassName("check-one"); //单选框
	var tr = table.children[1].rows; //行	
	var selectedTotal = document.getElementById('selectedTotal'); //已选商品数目容器
	var priceTotal = document.getElementById('priceTotal'); //总计
	var deleteAll = document.getElementById('deleteAll'); // 删除全部按钮
	var foot = document.getElementById('foot');

	var txt = localStorage.getItem("com_intro");
	var obj = JSON.parse(txt);
	var tr1;
	for(var i = 0; i < obj.length; i++) {
		tr1 = document.createElement("tr");
		tr1.innerHTML = '<td class="checkbox">' +
			'<input type="checkbox" class="check-one check"  />' +
			'</td>' +
			'<td class="goods">' +
			'<a href="pro_details.html"><img src="' + obj[i].img + '" alt="" /><span>' + obj[i].name + obj[i].span1 + obj[i].span2 + '</span></a><em>款式：' + obj[i].span1 + '</em><em>尺寸：' + obj[i].span2 + '</em>' +
			'</td>' +
			'<td class="pri">' +
			'￥<span class="price">' + obj[i].money + '</span>' +
			'</td>' +
			'<td class="count">' +
			'<span class="reduce">-</span>' + '<input class="count-input" type="text" value="' + obj[i].num + '">' + '<span class="add">+</span>' +
			'</td>' +
			'<td class="subt">' +
			'￥<span class="subtotal">' + obj[i].money * obj[i].num + '</span>' +
			'</td>' +
			'<td class="operation">' +
			'<span>加入收藏夹</span>' + '<span class="delete">删除</span>' +
			'</td>';
		tbody.appendChild(tr1);
	}

	var step = document.getElementById("qty"); //全部商品
	var len = tbody.children.length;
	step.innerText = len;

	// 更新总数和总价格
	function getTotal() {
		var seleted = 0;
		var price = 0;
		var HTMLstr = '';
		for(var i = 0, len = tr.length; i < len; i++) {
			if(tr[i].getElementsByTagName('input')[0].checked) {
				tr[i].className = 'on';
				seleted = tbody.getElementsByClassName("on").length;
				price += parseFloat(tr[i].getElementsByTagName('span')[4].innerHTML);
				HTMLstr += '<div><img src="' + tr[i].getElementsByTagName('img')[0].src + '"><span class="del" index="' + i + '"></span></div>'
			} else {
				tr[i].className = '';
			}
		}

		selectedTotal.innerHTML = seleted;
		priceTotal.innerHTML = price.toFixed(2);
		if(seleted == 0) {
			foot.className = 'foot';
		}
	}
	// 计算单行价格
	function getSubtotal(tr) {
		var cells = tr.cells;
		var price = cells[2] //单价
		var subtotal = cells[4]; //小计td
		var countInput = tr.getElementsByTagName('input')[1]; //数目input
		//写入HTML

		subtotal.innerHTML = "￥<span>" + (parseInt(countInput.value) * parseFloat(price.getElementsByTagName('span')[0].innerHTML)) + "</span>";
	}

	// 点击选择框
	var to = tbody.getElementsByClassName("on");
	var all0 = document.getElementById("all0");
	var all1 = document.getElementById("all1");

	for(var i = 0; i < selectInputs.length; i++) {
		selectInputs[i].onclick = function() {
			if(this.className.indexOf('check-all') >= 0) { //如果是全选，则吧所有的选择框选中
				for(var j = 0; j < selectInputs.length; j++) {
					selectInputs[j].checked = this.checked;
				}
			}
			if(!this.checked) { //只要有一个未勾选，则取消全选框的选中状态
				for(var i = 0; i < checkAllInputs.length; i++) {
					checkAllInputs[i].checked = false;
					all0.style.backgroundImage = "url(img/u2.png)";
					all1.style.backgroundImage = "url(img/u2.png)";
				}
			}
			getTotal(); //选完更新总计
			if(checkone.length == to.length) {
				for(var k = 0; k < selectInputs.length; k++) {
					selectInputs[k].checked = this.checked;
					all0.style.backgroundImage = "url(img/u3.png)";
					all1.style.backgroundImage = "url(img/u3.png)";
				}
			}
		}
	}
	//为每行元素添加事件
	for(var i = 0; i < tr.length; i++) {
		//将点击事件绑定到tr元素
		tr[i].onclick = function(e) {

			var e = e || window.event;
			var el = e.target || e.srcElement; //通过事件对象的target属性获取触发元素
			var cls = el.className; //触发元素的class
			var countInout = this.getElementsByTagName('input')[1]; // 数目input
			var value = parseInt(countInout.value); //数目

			//通过判断触发元素的class确定用户点击了哪个元素
			switch(cls) {
				case 'add': //点击了加号
					countInout.value = value + 1;
					getSubtotal(this);
					break;
				case 'reduce': //点击了减号
					if(value > 1) {
						countInout.value = value - 1;
						getSubtotal(this);
					}
					break;
				case 'delete': //点击了删除
					var conf = confirm('确定删除此商品吗？');
					if(conf) {
						var bb = $(this).index();
						obj.splice(bb, 1);
						var txt = JSON.stringify(obj);
						localStorage.setItem("com_intro", txt);

						this.parentNode.removeChild(this);
						len = tbody.children.length;
						step.innerText = len;
						var tnt = JSON.stringify(len);
						localStorage.setItem("bbb", tnt); //只写
						
						if(checkone.length == to.length) {
						
							$(".check-all").attr("checked","checked");
							all0.style.backgroundImage = "url(img/u3.png)";
							all1.style.backgroundImage = "url(img/u3.png)";
						
					}

					}

					if(tr.length == "0") {
						$(".check-all").removeAttr("checked");
						all0.style.backgroundImage = "url(img/u2.png)";
						all1.style.backgroundImage = "url(img/u2.png)";
					}
					
					break;

			}
			getTotal();
		}
		// 给数目输入框绑定keyup事件
		tr[i].getElementsByTagName('input')[1].onkeyup = function() {
			var val = parseInt(this.value);
			if(isNaN(val) || val <= 0) {
				val = 1;
			}
			if(this.value != val) {
				this.value = val;
			}
			getSubtotal(this.parentNode.parentNode); //更新小计
			getTotal(); //更新总数
		}
	}
	// 点击全部删除
	deleteAll.onclick = function() {
		if(selectedTotal.innerHTML != 0) {
			var con = confirm('确定删除所选商品吗？'); //弹出确认框
			if(con) {
				for(var i = 0; i < tr.length; i++) {
					// 如果被选中，就删除相应的行
					if(tr[i].getElementsByTagName('input')[0].checked) {
						obj.splice(0, 1);
						var txt = JSON.stringify(obj);
						localStorage.setItem("com_intro", txt);
						tr[i].parentNode.removeChild(tr[i]); // 删除相应节点
						i--; //回退下标位置
						len = tbody.children.length;
						step.innerText = len;
						var tnt = JSON.stringify(len);
						localStorage.setItem("bbb", tnt); //只写

					}
				}
			}
			if(tr.length == "0") {
				$(".check-all").removeAttr("checked");
				all0.style.backgroundImage = "url(img/u2.png)";
				all1.style.backgroundImage = "url(img/u2.png)";
			}
		} else {
			alert('请选择商品！');
		}

		getTotal(); //更新总数
	}

	//	 默认选第二个
	//	selectInputs[2].checked = true;
	//	selectInputs[2].onclick();

}